package com.hs.zhou.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.hs.dto.Constants;
import com.hs.dto.PageBean;
import com.hs.dto.ResultBean;
import com.hs.zhou.pojo.Bill;
import com.hs.zhou.pojo.BillStatement;
import com.hs.zhou.pojo.BillVo;
import com.hs.zhou.service.IBillService;
import com.hs.zhou.service.IBillStatementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Xudongzhou
 * @since 2022-06-14
 */
@CrossOrigin
@RestController
@RequestMapping("/bill")
public class BillController {

    @Autowired
    private IBillService billService;

    @Autowired
    private IBillStatementService billStatementService;

    @GetMapping("lists")
    public ResultBean findAll(Integer currentPage,  Integer pageSize, String orderId , String comsuName,Integer billStatus){

        System.out.println(currentPage);
        System.out.println(pageSize);

        PageInfo<Bill> pageInfo = billService.queryParamPage(currentPage, pageSize, orderId, comsuName, billStatus);

        List<Bill> records = pageInfo.getList();



        ResultBean resultBean = new ResultBean();

        if(!CollectionUtils.isEmpty(records)){
            PageBean<Bill> pageBean = new PageBean<>();
            pageBean.setList(records);
            pageBean.setTotal((int) pageInfo.getTotal());

            resultBean.setData(pageBean);
            resultBean.setMsg("查询成功");
            resultBean.setCode(Constants.SUCCESS_CODE);
            return resultBean;
        }
        return ResultBean.ERROR("查询失败");
    }

    @GetMapping("/list")
    public ResultBean findById(String id){
        Bill bill = billService.findById(id);
        ResultBean ok = ResultBean.OK("查询成功");
        ok.setData(bill);
        return ok;
    }

    @GetMapping("/sum")
    public ResultBean findByName(String comsuName){
        List<Bill> bills = billService.findAll(comsuName);

        BillVo billVo = new BillVo();

        Float totalReceive = 0f;

        Float totalFreight = 0f;

        Float totalExpense = 0f;

        Float totalAdvance = 0f;

        Float totalCollection = 0f;

        for(Bill bill:bills){
            totalReceive += bill.getExpenseinfo().getTotalReceivable();

            totalCollection += bill.getExpenseinfo().getCollectionAmount();

            totalAdvance += bill.getExpenseinfo().getAdvanceCharge();

            totalExpense += bill.getExpenseinfo().getTotalExpenses();

            totalFreight += bill.getExpenseinfo().getTotalFreight();
        }

        QueryWrapper<BillStatement> qrbs = new QueryWrapper<>();
        qrbs.eq("customer_name",comsuName);

        BillStatement billStatement = billStatementService.getOne(qrbs);

        billVo.setCustomerName(comsuName);
        billVo.setBillStatementNo(billStatement.getBillStatementNo());
        billVo.setTotalReceive(totalReceive);
        billVo.setTotalCollection(totalCollection);
        billVo.setTotalAdvance(totalReceive);
        billVo.setTotalExpense(totalExpense);
        billVo.setTotalFreight(totalFreight);

        ResultBean ok = ResultBean.OK("查询成功");
        ok.setData(billVo);
        return ok;
    }

    @PutMapping
    public ResultBean updateStatus(@RequestBody Bill bill){

        boolean b = billService.saveOrUpdate(bill);

        if(b){
            return ResultBean.OK("提交成功!");
        }
        return ResultBean.ERROR("提交失败");
    }

}
